const app = getApp()

const util = require('../../utils/util.js');
const API = require('../../config/api.js');

import Notify from '@vant/weapp/notify/notify';

function calcBegin() {
  var begin = new Date();
  var bm = (Math.ceil(begin.getMinutes() / 15) % 4) * 15
  if (begin.getMinutes() / 15 > 3)
    begin.setHours(begin.getHours() + 1);

  if (parseInt(begin.format("hhmm")) > 2200) {
    begin.setDate(begin.getDate() + 1)
    begin.setHours(8)
    begin.setMinutes(0)
  } else if (begin.getHours() < 8) {
    begin.setHours(8)
    begin.setMinutes(0)
  } else {
    begin.setMinutes(bm)
  }
  return begin;
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'长沙',
    label:'豫园中心',
    address:'长沙市岳麓区',
    branch:0,
    showTime: false,
    Seat_Current: null,
    start_at: new Date().format("hh:00"),
    duration: '1',
    Times: [
      { time: '07:00', disabled: false, Active: false, text: '7' },
      { time: '08:00', disabled: false, Active: false, text: '8' },
      { time: '09:00', disabled: false, Active: false, text: '9' },
      { time: '10:00', disabled: false, Active: false, text: '10' },
      { time: '11:00', disabled: false, Active: false, text: '11' },
      { time: '12:00', disabled: false, Active: false, text: '12' },
      { time: '13:00', disabled: false, Active: false, text: '13' },
      { time: '14:00', disabled: false, Active: false, text: '14' },
      { time: '15:00', disabled: false, Active: false, text: '15' },
      { time: '16:00', disabled: false, Active: false, text: '16' },
      { time: '17:00', disabled: false, Active: false, text: '17' },
      { time: '18:00', disabled: false, Active: false, text: '18' },
      { time: '19:00', disabled: false, Active: false, text: '19' },
      { time: '20:00', disabled: false, Active: false, text: '20' },
      { time: '21:00', disabled: false, Active: false, text: '21' },
      { time: '22:00', disabled: false, Active: false, text: '22' }
      // { time: '23:00', disabled: false, Active: false, text: '23' },
    ],
    currentDate: new Date().format('yyyy-MM-dd'),
    // currentTime: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    currentTime:'',
    minHour:  new Date().format("hh"),
    minMinute: new Date().format("mm"),
    maxHour: 23,
    currentEnd:'',
    duration:'',
    showTimes:false,
    durationShow:false,
    hours: ['1小时', '2小时', '3小时', '4小时', '5小时', '6小时', '7小时', '8小时', '9小时', '10小时', '11小时', '12小时', '13小时', '14小时', '15小时'],

  },
  ConfirmDate(e){
    app.globalData.recentOrder = {
      start_at: this.data.currentDate+" "+this.data.currentTime,
      end_at: this.data.currentEnd,
      duration: this.data.duration
    }
    wx.navigateTo({
      url: '/pages/book/seat',
    })
  },
  timeChange(e){
    console.log('e.detail.TimeRange',e.detail.TimeRange);
    this.setData({
      start_at: e.detail.TimeRange.substr(0,5),
      end_at: e.detail.TimeRange.substr(6,5),
      duration: e.detail.duration
    })
  },
  onConfirm(e){
    console.log(e.detail)
    this.setData({
      currentDate: new Date(e.detail).format('yyyy-MM-dd'),
      showTime: false
    });
  },
  onTimeClose(){
    this.setData({
      showTime: false
    })
  },
  bindTime(){
    this.setData({
      showTimes:true,
      minHour:  new Date().format("hh"),
      minMinute: new Date().format("mm"),
    })
  },
  onTimesClose(){
    this.setData({
      showTimes:false
    })
  },
  onInput(event) {
    this.setData({
      currentTime: event.detail,
      showTimes:false
    });
  },
  bindEnd(){
this.setData({
  showEnd:true
})
  },

  /**
   * 选择日期
   * @param e 
   */
  bindDatas(e) {
    this.setData({
      showTime: true
    })
  },
  bindDuration(){
    this.setData({
      durationShow:true
    })
  },
  onDuration(){
    this.setData({
      durationShow:false
    })
  },
  choseHour(event){
    this.data.duration = event.detail.index + 1
    this.calcTerminal();
    this.setData({
      durationShow: false
    })
  },
  calcTerminal() {
    var self = this;
    const {currentTime,duration} = this.data;
    util.request(
      API.OrderCalcTerm,
      { start_at: currentTime, duration:duration}
    ).then(res => {
      if(res.result == 'success') {
        if(res.data.msg) {
          Notify({
            top: "150rpx",
            type:"warning",
            message: res.data.msg
          })
        }
        
        self.setData({
          duration:res.data.duration,
          currentEnd:res.data.end_at
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var suggest = calcBegin();
    this.setData({
      currentTime: suggest.format("hh:mm"),
      duration:2,
    })

    this.calcTerminal();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})